<template>
    <div class="register-container">
        <div class="register-card">
            <div class="register-header">
                <h1>蜗牛AI在线课堂</h1>
                <p>创建您的账户，开始学习之旅</p>
            </div>

            <a-form :model="form" :rules="rules" @finish="handleRegister" layout="vertical">
                <a-form-item label="用户名" name="username">
                    <a-input v-model:value="form.username" placeholder="请输入用户名" size="large">
                        <template #prefix>
                            <UserOutlined />
                        </template>
                    </a-input>
                </a-form-item>

                <a-form-item label="邮箱" name="email">
                    <a-input v-model:value="form.email" placeholder="请输入邮箱" size="large">
                        <template #prefix>
                            <MailOutlined />
                        </template>
                    </a-input>
                </a-form-item>

                <a-form-item label="手机号" name="phone">
                    <a-input v-model:value="form.phone" placeholder="请输入手机号" size="large">
                        <template #prefix>
                            <PhoneOutlined />
                        </template>
                    </a-input>
                </a-form-item>

                <a-form-item label="密码" name="password">
                    <a-input-password v-model:value="form.password" placeholder="请输入密码" size="large">
                        <template #prefix>
                            <LockOutlined />
                        </template>
                    </a-input-password>
                </a-form-item>

                <a-form-item label="确认密码" name="confirmPassword">
                    <a-input-password v-model:value="form.confirmPassword" placeholder="请再次输入密码" size="large">
                        <template #prefix>
                            <LockOutlined />
                        </template>
                    </a-input-password>
                </a-form-item>

                <a-form-item name="agreement">
                    <a-checkbox v-model:checked="form.agreement">
                        我已阅读并同意
                        <a @click="showAgreement">《用户协议》</a>
                        和
                        <a @click="showPrivacy">《隐私政策》</a>
                    </a-checkbox>
                </a-form-item>

                <a-form-item>
                    <a-button type="primary" html-type="submit" size="large" block :loading="loading">
                        注册
                    </a-button>
                </a-form-item>
            </a-form>

            <div class="register-footer">
                <span>已有账户？</span>
                <a @click="$router.push('/login')">立即登录</a>
            </div>
        </div>
    </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { message } from 'ant-design-vue'
import { UserOutlined, MailOutlined, PhoneOutlined, LockOutlined } from '@ant-design/icons-vue'
import {register} from '@/api/loginController'

const router = useRouter()
const loading = ref(false)

const form = reactive({
    username: '',
    email: '',
    phone: '',
    password: '',
    confirmPassword: '',
    agreement: false
})

const validateConfirmPassword = async (_rule: any, value: string) => {
  if (!value) {
    return Promise.reject('请确认密码')
  }
  if (value !== form.password) {
    return Promise.reject('两次输入的密码不一致')
  }
  return Promise.resolve()
}

const rules = {
    username: [
        { required: true, message: '请输入用户名', trigger: 'blur' },
        { min: 3, max: 20, message: '用户名长度在3-20个字符之间', trigger: 'blur' }
    ],
    email: [
        { required: true, message: '请输入邮箱', trigger: 'blur' },
        { type: 'email', message: '请输入正确的邮箱格式', trigger: 'blur' }
    ],
    phone: [
        { required: true, message: '请输入手机号', trigger: 'blur' },
        { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号格式', trigger: 'blur' }
    ],
    password: [
        { required: true, message: '请输入密码', trigger: 'blur' },
        { min: 6, message: '密码长度不能少于6位', trigger: 'blur' }
    ],
    confirmPassword: [
        { required: true, message: '请确认密码', trigger: 'blur' },
        { validator: validateConfirmPassword, trigger: 'blur' }
    ],
   agreement: [
    {
    validator: (_rule: any, value: boolean) => {
      return value
        ? Promise.resolve()
        : Promise.reject('请阅读并同意用户协议和隐私政策')
    },
    trigger: 'change'
    }
    ]
}

const handleRegister = async (values: any) => {
  loading.value = true
try {
    const {data} = await register(values);  
    console.log(data);
    if (data.code === 0) {
      message.success('注册成功');
      router.push('/login');
    } else {
      message.error(data.message || '注册失败');
    }
  } catch (error) {
    message.error('请求失败，请稍后重试');
    console.error('注册错误:', error);
  } finally {
    loading.value = false;
  }
};


const showAgreement = () => {
    message.info('用户协议内容')
}

const showPrivacy = () => {
    message.info('隐私政策内容')
}
</script>

<style scoped>
.register-container {
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    padding: 24px;
}

.register-card {
    background: #fff;
    border-radius: 12px;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
    padding: 48px;
    width: 100%;
    max-width: 480px;
}

.register-header {
    text-align: center;
    margin-bottom: 32px;
}

.register-header h1 {
    color: #333;
    margin-bottom: 8px;
    font-size: 28px;
}

.register-header p {
    color: #666;
    margin: 0;
}

.register-footer {
    text-align: center;
    margin-top: 24px;
    color: #666;
}

.register-footer a {
    color: #1890ff;
    margin-left: 4px;
}
</style>